<div class="layui-card">
  <form class="layui-form" lay-filter="add-goods-form">
    <blockquote class="layui-elem-quote">顶图设置</blockquote>
    <div class="layui-field-box">
	    <div class="layui-form-item">
	      <label class="layui-form-label">顶图设置</label>
	      <div class="layui-input-inline">
	          <button type="button" class="layui-btn" id="add-goods-upload-btn">
	            <i class="layui-icon">&#xe67c;</i>上传图片
	          </button>
	          <div class="layui-upload-list">
	            <img class="layui-upload-img" src="" id="add-goods-upload-img" style="width: 700px;">
	            <input type="hidden" name="top_image" id="add-goods-upload-image" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
	          </div>
	      </div>
	        <div class="layui-form-mid layui-word-aux"></div>
	    </div>
	    <div class="layui-form-item" style="text-align: center">
	        <div class="layui-input-block">
	            <button class="layui-btn" lay-submit lay-filter="formDemo2">保存</button>
	        </div>
	    </div>
    </div>
  </form>

<script>
    //Demo
  layui.use(['form', 'upload'], function(){
    var form = layui.form;
    var upload = layui.upload
    var $ = layui.$;
    //上传图片
  upload.render({
	elem: '#add-goods-upload-btn',
	url: 'https://bookshop.dingdangtoy.com/api/admin/image/store',
	accept: 'images',
	method: 'get',
	acceptMime: 'image/*',
	done: function(res) {
		$('#add-goods-upload-img').attr('src', res.data.url);
		$('#add-goods-upload-image').val(res.data.url)
		//$(this.item).prev("div").children("input").val(res.data.url)
	}
  }); 
    init();
    function init() {
	   	  $.ajax({
				    type: "GET",
				    dataType: "json",
				    url: "https://bookshop.dingdangtoy.com/api/admin/book_list/top_image",
				    data: {},
				    headers: { //通过 request 头传递
		              'access-token': layui.data('layuiAdmin').access_token
		            },
					success: function(data){
					  $('#add-goods-upload-img').attr('src',data.data.top_image)
					    
					},
					error: function(msg){
					  console.log(msg)
					}
				});      	
    }   
	  // 监听编辑提交
	  form.on('submit(formDemo2)', function(data){	
        //执行 Ajax 后重载
   	  $.ajax({
			    type: "POST",
			    dataType: "json",
			    url: "https://bookshop.dingdangtoy.com/api/admin/book_list/top_image",
			    data: {
			    	top_image:data.field.top_image,
			    },
			    headers: { //通过 request 头传递
	              'access-token': layui.data('layuiAdmin').access_token
	            },
				success: function(data){
				  if(data.code == 0){
				  	init();
				  	layer.msg('操作成功');
				  } else {
				  	layer.msg(data.message);
				  }
				    
				},
				error: function(msg){
				  console.log(msg)
				}
			});  

	    return false;
	  });
	  
  });
</script>
<style>
    .layui-form-label {text-align: center; width: 170px;}
    .freight-top{
    	height: 60px;
    	line-height: 60px;
    	padding: 0 106px;
    	margin-bottom: 20px;
    	font-size: 18px;
    	border-bottom: 1px solid #000000;
    }
    .int1{position: relative;padding-left: 30px;}
    .sp3{position: absolute;left: 10px;top: 10px;z-index: 10;color: #999;}
</style>